# -*- coding:utf-8 -*-
import logging
import os
import sys
from flask_migrate import MigrateCommand
current_pwd = os.getcwd()
from flask_script import Manager, Shell
# from conn.mysql import conn_mysql
from apps import app


manager = Manager(app)
manager.add_command('db', MigrateCommand)


# @app.teardown_request
# def teardown_request(exception):
#     print "----close mysql conn-----"
#     app.logger.error(exception)
#     conn_mysql.remove()
#     if exception and conn_mysql.is_active:
#         conn_mysql.rollback()

@app.after_request
def app_after_request(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    return response


def make_shell_context():
    return dict(app=app)


manager.add_command('shell', Shell(make_context=make_shell_context))
if __name__ == "__main__":
    logfile = "./jstree.log"
    app.debug = True
    logging.basicConfig(filename=logfile, level=logging.INFO)
    logger = logging.getLogger('werkzeug')
    logger.setLevel(logging.INFO)
    manager.run()
